// rollup配置文件
// 根据环境变量中的target属性，获取对应模块中的package.json

import path from 'path';
import {rollup} from "rollup";

const packagesDir = path.resolve(__dirname, 'packages');

// 打包的基准目录
const packageDir = path.resolve(packagesDir, process.env.TARGET); // 要打包的目录

/**
 * 永远针对的是某个模块
 * @param p
 * @returns {string}
 */
const resolve = (p) => path.resolve(packageDir, p);

const pkg = require(resolve('package.json'));

// 对打包的类型进行映射，根据提供的formats来格式化需要打包的内容
const outputName = path.basename(packageDir);
const outputConfig = {
    'esm-bundler': {
        file: resolve(`dist/${outputName}.esm-bundler.js`),
        format: 'es'
    },
    'cjs': {
        file: resolve(`dist/${outputName}.cjs.js`),
        format: 'cjs'
    },
    'global': {
        file: resolve(`dist/${outputName}.global.js`),
        format: 'iife' // 立即执行函数
    }
}

const options = pkg.buildOptions

function createConfig(format, output) {
    output.name = options.name;
    output.sourcemap = true; // 生成sourcemap
    console.log(output)
    // 生成rollup配置
}

// rollup 最终需要导出配置
export default options.formats.map((format) => {
    createConfig(format, outputConfig[format])
})